﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class KaryawanAdd : Form
    {
        public KaryawanAdd()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btnBatal_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTambah_Click(object sender, EventArgs e)
        {
            if (false == checkFieldData())
            {
                MessageBox.Show("Harap isi semua data !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            DataTable dt = Database.getInstance().query("select * from Employee");

            DataRow dataRow = dt.NewRow();
            dataRow["emp_name"] = this.txtNama.Text;

            if (this.cmbKelamin.Text == "Laki-laki")
            {
                dataRow["emp_gender"] = "L";
            }
            else
            {
                dataRow["emp_gender"] = "L";
            }
           
            dataRow["emp_birthdate"] = this.birthdayBox.Value.Date;
            dataRow["emp_birthplace"] = this.textBirthPlace.Text;
            dataRow["emp_address"] = this.txtAlamat.Text;
            dataRow["emp_role"] = this.textJabatan.Text;
            dataRow["emp_edu"] = this.textPendidikan.Text;
            dataRow["emp_start"] = this.startWorkBox.Value.Date;
            dataRow["emp_duration"] = num1.Value + " - " + num2.Value; 
            dataRow["emp_yayasan"] = this.txtSK.Text;
            dataRow["emp_phone"] = this.textPhone.Text;
            dataRow["emp_mobile"] = this.textMobile.Text;
            dataRow["emp_email"] = this.textEmail.Text;
            
            dt.Rows.Add(dataRow);

            Database.getInstance().updateDB(dt);

            var result = MessageBox.Show("Tambah karyawan berhasil !", "Sukses !", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                this.Close();
            }
        }

        private Boolean checkFieldData()
        {
            if (this.txtNama.Text == "" ||
                this.cmbKelamin.Text == "" ||
                this.textBirthPlace.Text == "" ||
                this.textJabatan.Text == "" ||
                this.textPendidikan.Text == "" ||
                this.txtAlamat.Text == "" ||
                this.txtSK.Text == "" ||
                this.textPhone.Text == "" ||
                this.textMobile.Text == "" ||
                this.textEmail.Text == "")
            {
                return false;
            }
            return true;
        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void KaryawanAdd_Load(object sender, EventArgs e)
        {
            DateTime date = DateTime.Now;
            if (date.Month < 7)
            {
                num1.Value = date.Year - 1;
                num2.Value = num1.Value + 1;
            }
        }

        private void num1_ValueChanged(object sender, EventArgs e)
        {
            num2.Value = num1.Value + 1;
        }

        private void num2_ValueChanged(object sender, EventArgs e)
        {
            num1.Value = num2.Value - 1;
        }
    }
}
